package cn.fate.core.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    public static final String MONTH_PATTERN = "yyyy-MM";
    public static final long SECOND = 1000L;
    public static final long MINUTE = 60000L;
    public static final long HOUR = 3600000L;
    public static final long DAY = 86400000L;
    public static final String DEFAULT_PATTERN = "yyyyMMdd";
    public static final String SHORT_PATTERN = "yyyy-MM-dd";
    public static final String FULL_PATTERN = "yyyyMMddHHmmss";
    public static final String FULL_PATTERN_HOUR = "yyyyMMddHHmm";
    public static final String FULL_STANDARD_PATTERN = "yyyyMMdd HH:mm:ss";

    private DateUtil() {
    }

    public static String getShortNow() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

    public static String getTimeBykm() {
        SimpleDateFormat formatter = new SimpleDateFormat("H:mm");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

    public static String getMonth() {
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

    public static String getDay() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

    public static String formatDate(final Date date) {
        return formatDate(date, "yyyyMMdd");
    }

    public static String formatShortDate(final Date date) {
        return formatDate(date, "yyyy-MM-dd");
    }

    public static String formatDate(final Date date, String format) {
        return (new SimpleDateFormat(format)).format(date);
    }

    public static String formatDate(final long timeMillis) {
        return formatDate(timeMillis, "yyyy-MM-dd H:mm:ss");
    }

    public static String formatDate(final long timeMillis, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date nowc = new Date(timeMillis);
        String pid = formatter.format(nowc);
        return pid;
    }

    public static Date parseDate(String date) {
        return parseDate(date, "yyyyMMdd", (Date)null);
    }

    public static Date parseShortDate(String date) {
        return parseDate(date, "yyyy-MM-dd", (Date)null);
    }

    public static Date parseDate(String date, String df) {
        return parseDate(date, df, (Date)null);
    }

    public static Date parseDate(String date, String df, Date defaultValue) {
        if (date == null) {
            return defaultValue;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(df);

            try {
                return formatter.parse(date);
            } catch (ParseException var5) {
                return defaultValue;
            }
        }
    }

    public static Date currentDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(getCurTime());
        calendar.set(10, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new Date(calendar.getTimeInMillis());
    }

    public static Date getStartOfDate(final Date date) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        cal.set(11, 0);
        cal.set(12, 0);
        cal.set(13, 0);
        cal.set(14, 0);
        return new Date(cal.getTime().getTime());
    }

    public static Date getPreviousMonday() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(7) - 1;
        Date date;
        if (dayOfWeek == 1) {
            date = addDays(cd.getTime(), -7);
        } else {
            date = addDays(cd.getTime(), -6 - dayOfWeek);
        }

        return getStartOfDate(date);
    }

    public static Date getMondayBefore4Week() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(7) - 1;
        Date date;
        if (dayOfWeek == 1) {
            date = addDays(cd.getTime(), -28);
        } else {
            date = addDays(cd.getTime(), -27 - dayOfWeek);
        }

        return getStartOfDate(date);
    }

    public static Date getCurrentMonday() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(7) - 1;
        Date date;
        if (dayOfWeek == 1) {
            date = cd.getTime();
        } else {
            date = addDays(cd.getTime(), 1 - dayOfWeek);
        }

        return getStartOfDate(date);
    }

    public static boolean beforeDay(final Date date1, final Date date2) {
        return getStartOfDate(date1).before(getStartOfDate(date2));
    }

    public static boolean afterDay(final Date date1, final Date date2) {
        return getStartOfDate(date1).after(getStartOfDate(date2));
    }

    public static Date addMonths(Date date, int months) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(2, months);
        return cal.getTime();
    }

    public static Date addDays(final Date date, int days) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        cal.add(5, days);
        return new Date(cal.getTime().getTime());
    }

    public static Date addMins(final Date date, int mins) {
        Calendar cal = GregorianCalendar.getInstance();
        cal.setTime(date);
        cal.add(12, mins);
        return new Date(cal.getTime().getTime());
    }

    public static boolean isSameMonth(Date date1, Date date2) {
        Calendar cal1 = GregorianCalendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = GregorianCalendar.getInstance();
        cal2.setTime(date2);
        return isSameMonth(cal1, cal2);
    }

    public static boolean isSameMonth(Calendar cal1, Calendar cal2) {
        return cal1.get(1) == cal2.get(1) && cal1.get(2) == cal2.get(2);
    }

    public static Date getEndOfMonth(final Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(2, calendar.get(2) + 1);
        calendar.set(5, 0);
        calendar.set(11, 12);
        calendar.set(12, 0);
        calendar.set(13, 0);
        calendar.set(14, 0);
        return new Date(calendar.getTimeInMillis());
    }

    public static Date getFirstOfMonth(final Date date) {
        Date lastMonth = addMonths(date, -1);
        lastMonth = getEndOfMonth(lastMonth);
        return addDays(lastMonth, 1);
    }

    public static void main(String[] args) {
        System.out.println("formatDate(new Date(), \"yyyyMMdd\"):" + formatDate(new Date(), "yyyyMMdd"));
    }

    public static boolean inFormat(String sourceDate, String format) {
        if (sourceDate == null) {
            return false;
        } else {
            try {
                SimpleDateFormat dateFormat = new SimpleDateFormat(format);
                dateFormat.setLenient(false);
                dateFormat.parse(sourceDate);
                return true;
            } catch (Exception var3) {
                var3.printStackTrace();
                return false;
            }
        }
    }

    public static String now() {
        return formatDate(new Date(), "yyyyMMddHHmmss");
    }

    public static String formatShortDateC(Date gstrDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String pid = formatter.format(gstrDate);
        return pid;
    }

    public static String getNow() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd H:mm:ss");
        Date nowc = new Date();
        String pid = formatter.format(nowc);
        return pid;
    }

    public static String formatShort(String strDate) {
        String ret = "";
        if (strDate != null && !"1900-01-01 00:00:00.0".equals(strDate) && strDate.indexOf("-") > 0) {
            ret = strDate;
            if (strDate.indexOf(" ") > -1) {
                ret = strDate.substring(0, strDate.indexOf(" "));
            }
        }

        return ret;
    }

    public static int getNumberOfSecondsBetween(final double d1, final double d2) {
        return d1 != 0.0D && d2 != 0.0D ? (int)(Math.abs(d1 - d2) / 1000.0D) : -1;
    }

    public static int getNumberOfMonthsBetween(final Date before, final Date end) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(before);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(end);
        return (cal2.get(1) - cal1.get(1)) * 12 + (cal2.get(2) - cal1.get(2));
    }

    public static long getNumberOfMinuteBetween(final Date before, final Date end) {
        long millisec = end.getTime() - before.getTime();
        return millisec / 60000L;
    }

    public static String formatMatchTime(Date srcDate) {
        Calendar cal1 = GregorianCalendar.getInstance();
        cal1.setTime(srcDate);
        Calendar cal2 = GregorianCalendar.getInstance();
        SimpleDateFormat formatter;
        if (isSameDay(cal1, cal2)) {
            formatter = new SimpleDateFormat("今天 H:mm");
        } else if (isTomorrow(srcDate)) {
            formatter = new SimpleDateFormat("明天 H:mm");
        } else {
            formatter = new SimpleDateFormat("MM月dd日 H:mm");
        }

        return formatter.format(srcDate);
    }

    private static boolean isTomorrow(Date srcDate) {
        Calendar cal1 = GregorianCalendar.getInstance();
        cal1.add(5, 1);
        Calendar cal2 = GregorianCalendar.getInstance();
        cal2.setTime(srcDate);
        return isSameDay(cal1, cal2);
    }

    private static boolean isSameDay(Calendar cal1, Calendar cal2) {
        return cal1.get(1) == cal2.get(1) && cal1.get(2) == cal2.get(2) && cal1.get(5) == cal2.get(5);
    }

    public static long getNumberOfDaysBetween(Calendar cal1, Calendar cal2) {
        cal1.clear(14);
        cal1.clear(13);
        cal1.clear(12);
        cal1.clear(11);
        cal2.clear(14);
        cal2.clear(13);
        cal2.clear(12);
        cal2.clear(11);
        long elapsed = cal2.getTime().getTime() - cal1.getTime().getTime();
        return elapsed / 86400000L;
    }

    public static Long date2Long() {
        return date2Long(new Date(), "yyyyMMddHHmmss");
    }

    public static Long date2Long(Date date) {
        return date2Long(date, "yyyyMMddHHmmss");
    }

    public static Long date2Long(Date date, String formatStr) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatStr);
        String dateString = simpleDateFormat.format(date);
        return Long.valueOf(dateString);
    }

    public static Date Long2Date(Long dt) {
        int s = (int)(dt % 100L);
        dt = dt / 100L;
        int m = (int)(dt % 100L);
        dt = dt / 100L;
        int h = (int)(dt % 100L);
        dt = dt / 100L;
        int d = (int)(dt % 100L);
        dt = dt / 100L;
        int month = (int)(dt % 100L);
        int year = (int)(dt / 100L);
        Calendar day = Calendar.getInstance();
        day.set(1, year);
        day.set(2, month - 1);
        day.set(5, d);
        day.set(11, h);
        day.set(12, m);
        day.set(13, s);
        day.set(14, 0);
        return day.getTime();
    }

    public static Date LongMinute2Date(Long dt) {
        int m = (int)(dt % 100L);
        dt = dt / 100L;
        int h = (int)(dt % 100L);
        dt = dt / 100L;
        int d = (int)(dt % 100L);
        dt = dt / 100L;
        int month = (int)(dt % 100L);
        int year = (int)(dt / 100L);
        Calendar day = Calendar.getInstance();
        day.set(1, 2000 + year);
        day.set(2, month - 1);
        day.set(5, d);
        day.set(11, h);
        day.set(12, m);
        day.set(13, 0);
        day.set(14, 0);
        return day.getTime();
    }

    public static int getDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(7);
    }

    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(7);
    }

    public static int getDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(6);
    }

    public static int getDayOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(6);
    }

    public static int getWeekOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.setFirstDayOfWeek(2);
        return calendar.get(3);
    }

    public static int getWeekOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(2);
        return calendar.get(3);
    }

    public static long getDaysDiff(Date startDate, Date endDate) {
        Calendar fromCalendar = Calendar.getInstance();
        fromCalendar.setTime(startDate);
        fromCalendar.set(11, 0);
        fromCalendar.set(12, 0);
        fromCalendar.set(13, 0);
        fromCalendar.set(14, 0);
        Calendar toCalendar = Calendar.getInstance();
        toCalendar.setTime(endDate);
        toCalendar.set(11, 0);
        toCalendar.set(12, 0);
        toCalendar.set(13, 0);
        toCalendar.set(14, 0);
        return (toCalendar.getTime().getTime() - fromCalendar.getTime().getTime()) / 86400000L;
    }

    public static long getCurTime() {
        return System.currentTimeMillis();
    }
}
